package com.zxl.live.screen.screen.utils;

import android.content.Context;
import android.util.DisplayMetrics;

public class ScreenUtils {
	
	private static final int DEFAULT_WIDTH_PIXELS = 720;
	
	private static final int DEFAULT_HEIGHT_PIXELS = 1280;
	
	private static final float NUM_5F = 0.5f;
	
	public static float sDensity = 1.0f;
	
	public static int sDensityDpi;
	
	public static float sFontDensity;
	
	public static int sWidthPixels = DEFAULT_WIDTH_PIXELS;		//宽度
	public static int sHeightPixels = DEFAULT_HEIGHT_PIXELS;	//高度
	
	public static float sScaleX = 1.0f; // 非默认屏幕宽度分辨率与默认分辨率的比例（默认720）
	public static float sScaleY = 1.0f; // 非默认屏幕高度度分辨率与默认分辨率的比例（默认1280）
	public static float sScaleXY = 1.0f; // 非默认屏幕高度度分辨率与默认分辨率的比例（默认720*1280）

	public static int sBaseWidthPixels = DEFAULT_WIDTH_PIXELS;		//虚拟屏幕宽度
	public static int sBaseHeightPixels = DEFAULT_HEIGHT_PIXELS;	//虚拟屏幕高度
	
	public static void reset(Context mContext) {
		if (mContext != null && null != mContext.getResources()) {
			DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
			sDensity = metrics.density;
			sFontDensity = metrics.scaledDensity;
			sWidthPixels = metrics.widthPixels;
			sHeightPixels = metrics.heightPixels;
			sDensityDpi = metrics.densityDpi;
			final boolean isLand = sWidthPixels > sHeightPixels;
			sBaseWidthPixels = isLand ? DEFAULT_HEIGHT_PIXELS : DEFAULT_WIDTH_PIXELS;
			sBaseHeightPixels = isLand ? DEFAULT_WIDTH_PIXELS : DEFAULT_HEIGHT_PIXELS;
			sScaleX = (float) sWidthPixels / sBaseWidthPixels;
			sScaleY = (float) sHeightPixels / sBaseHeightPixels;
			sScaleXY = sScaleX > sScaleY ? sScaleX : sScaleY;
		}
	}
	
	public static boolean isPort() {
		return sWidthPixels < sHeightPixels;
	}
	
	/**
	 * 将dp转换为px
	 * @param dipValue
	 * @return
	 */
	public static int dip2px(float dipValue) {
		return (int) (dipValue * sDensity + NUM_5F);
	}

	/**
	 * 将px转换为dp
	 * @param pxValue
	 * @return
	 */
	public static int px2dip(float pxValue) {
		return (int) (pxValue / sDensity + NUM_5F);
	}
	
	public static float getHeight(float height) {
		if (sHeightPixels == 0) {
			throw new RuntimeException("ScreenUtils not init");
		}
		return height * sHeightPixels / DEFAULT_HEIGHT_PIXELS;
	}
}
